﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CST_ImageDb.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		Dialog for 3D points settings
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   
'
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"

Imports System.Windows.Forms

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '




'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '

Public Class CST_ImageDb

    '// --------------------------------------------------------------------- '
    '// DATA
    Private LocFavL() As FAVIMG = Nothing
    Private m_SortingColumn As ColumnHeader
    '// ===================================================================== '


    ' /////////////////////////////////////////////////////////////////////////
    #Region "Private functions"

    #End Region
    ' /////////////////////////////////////////////////////////////////////////


    ' /////////////////////////////////////////////////////////////////////////
    #Region "Public functions"

    #End Region
    ' /////////////////////////////////////////////////////////////////////////


    ' /////////////////////////////////////////////////////////////////////////
    #Region "Event handlers"

    '// --------------------------------------------------------------------- '
    Private Sub AddOne()
        Dim iIdx As Integer

        '// get the integer in question
        Try
            iIdx = CInt(Me.TX_NewFav.Text)
        Catch ex As Exception
            Autodesk.AutoCAD.ApplicationServices.Application. _
                ShowAlertDialog(CMain._T(TX.NAN))
            Me.TX_NewFav.SelectAll()
            Me.TX_NewFav.Focus()
            Return
        End Try

        Dim li As System.Windows.Forms.ListViewItem
        Dim sData As DBDATA = Nothing
        Dim bOk As Boolean
        sData.ExcelWorkbook = Nothing

        If (iIdx < CM_ImageDb.DBF_Row_1) Then
            Autodesk.AutoCAD.ApplicationServices.Application. _
                ShowAlertDialog( _
                    String.Format( _
                        CMain._T(TX.IMDB_EMINI), _
                        iIdx, CM_ImageDb.DBF_Row_1))
            Me.TX_NewFav.SelectAll()
            Me.TX_NewFav.Focus()
            Return
        End If
        
        '// check if this one is already in the list
        If (LocFavL isnot Nothing) Then
        For Each sE As FAVIMG In LocFavL
            If (sE.idx = iIdx) Then
                Autodesk.AutoCAD.ApplicationServices.Application. _
                    ShowAlertDialog(CMain._T(TX.IMDB_ERRPRES))
                Return
            End If
        Next
        end if

        Try
            '// start the database
            If (CM_ImageDb.Start_DB(sData) = True) Then

                If (iIdx >= sData.img_cnt + CM_ImageDb.DBF_Row_1) Then
                    Autodesk.AutoCAD.ApplicationServices.Application. _
                        ShowAlertDialog( _
                            String.Format( _
                                CMain._T(TX.IMDB_EMAXI), _
                                iIdx, sData.img_cnt + CM_ImageDb.DBF_Row_1))
                    Me.TX_NewFav.SelectAll()
                    Me.TX_NewFav.Focus()
                    Exit Try
                End If

                If (LocFavL Is Nothing) Then
                    ReDim LocFavL(0 To 0)
                Else
                    ReDim Preserve LocFavL(0 To LocFavL.Length)
                End If
                With LocFavL(LocFavL.Length - 1)
                    .idx = iIdx


                    bOk = True
                    '// bOk is only modified (to False) in case of failure
                    .id.sName = _
                        sData.ExcelSheet.GetCVStr( _
                        .idx, CM_ImageDb.DBF_ColName)
                    .id.sPath = _
                        sData.ExcelSheet.GetCVStr( _
                        .idx, CM_ImageDb.DBF_ColPath)
                    .id.dNorth = _
                        sData.ExcelSheet.GetCVDbl( _
                        .idx, CM_ImageDb.DBF_ColNins, bOk)
                    .id.dEast = _
                        sData.ExcelSheet.GetCVDbl( _
                        .idx, CM_ImageDb.DBF_ColEins, bOk)
                    .id.dScale = _
                        sData.ExcelSheet.GetCVDbl( _
                        .idx, CM_ImageDb.DBF_ColScl, bOk)
                    .id.dRot = _
                        sData.ExcelSheet.GetCVDbl( _
                        .idx, CM_ImageDb.DBF_ColRot, bOk)

                    If (bOk = False) Then
                        Autodesk.AutoCAD.ApplicationServices.Application. _
                            ShowAlertDialog(CMain._T(TX.IMDB_EGETD))
                        If (LocFavL.Length = 1) Then
                            LocFavL = Nothing
                        Else
                            ReDim Preserve LocFavL(0 To LocFavL.Length - 2)
                        End If
                    Else
                        li = Me.LW_Fav.Items.Add(.idx.ToString())
                        li.SubItems.Add(.id.sName)
                        li.SubItems.Add(.id.sPath)
                        li.SubItems.Add(CStr(.id.dNorth))
                        li.SubItems.Add(CStr(.id.dEast))
                        li.SubItems.Add(CStr(.id.dScale))
                        li.SubItems.Add(CStr(.id.dRot))
                    End If
                End With

            End If
        Catch ex As System.Exception
        Finally
            If (sData.ExcelWorkbook IsNot Nothing) Then
                CM_ImageDb.Stop_DB(sData)
            End If
        End Try

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub OK_Button_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) _
                                Handles OK_Button.Click

        CM_ImageDb.Stgs.sDBPath = Me.tx_DB.Text
        CM_ImageDb.Stgs.sFavStr = LocFavL
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub Cancel_Button_Click(ByVal sender As System.Object, _
                                    ByVal e As System.EventArgs) _
                                    Handles Cancel_Button.Click

        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CST_ImageDb_Load( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load

        Me.Text = CMain._T(TX.IMGDBF) & ". " & _
                CMain._T(TX.SETTINGS) & "."
        Me.OK_Button.Text = CMain._T(TX.OK)
        Me.Cancel_Button.Text = CMain._T(TX.CANCEL)

        Me.TP_Dbf.Text = CMain._T(TX.DBF)
        Me.LB_Loc.Text = CMain._T(TX.PATH)
        Me.B_View.Text = CMain._T(TX.VIEW)
        Me.B_Browse.Text = CMain._T(TX.CHOOSE)

        Me.tx_DB.Text = CM_ImageDb.Stgs.sDBPath


        Me.TP_Fav.Text = CMain._T(TX.FAV)
        Me.TX_FavInfo.Text = CMain._T(TX.FAV_INF)
        Me.B_FavAdd.Text = CMain._T(TX.ADD)
        Me.B_FavRem.Text = CMain._T(TX.REMOVE)

        '// Set-up the list of favorite images
        Me.LW_Fav.CheckBoxes = False
        Me.LW_Fav.Columns.Add("#", 40)
        Me.LW_Fav.Columns.Add(CMain._T(TX.FILE), 180)
        Me.LW_Fav.Columns.Add(CMain._T(TX.PATH), 200)
        Me.LW_Fav.Columns.Add(CMain._T(TX.NORTH), 60)
        Me.LW_Fav.Columns.Add(CMain._T(TX.EAST), 60)
        Me.LW_Fav.Columns.Add(CMain._T(TX.SCALE), 60)
        Me.LW_Fav.Columns.Add(CMain._T(TX.ROT), 60)

        '// make shure the list of favorite images is loaded with info
        CM_ImageDb.GetFavInfo()


        LocFavL = CM_ImageDb.Stgs.sFavStr

        '// load favorites
        If (LocFavL IsNot Nothing) Then
            Dim li As System.Windows.Forms.ListViewItem
            For Each sE As FAVIMG In LocFavL
                If (sE.idx > 0) Then
                    li = Me.LW_Fav.Items.Add(sE.idx.ToString())
                    li.SubItems.Add(sE.id.sName)
                    li.SubItems.Add(sE.id.sPath)
                    li.SubItems.Add(CStr(sE.id.dNorth))
                    li.SubItems.Add(CStr(sE.id.dEast))
                    li.SubItems.Add(CStr(sE.id.dScale))
                    li.SubItems.Add(CStr(sE.id.dRot))
                End If
            Next
        End If

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_View_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_View.Click

		Try
            If System.IO.File.Exists(Me.tx_DB.Text) = False Then
                CMain.AlertUser(CMain._T(TX.IMGE_DB) & vbNewLine & _
                    Me.tx_DB.Text & vbNewLine & _
                    CMain._T(TX.ERR_FILE))
                Exit Try
            End If
            Dim psi As New ProcessStartInfo()
            With psi
                .FileName = CM_ImageDb.Stgs.sDBPath
                .UseShellExecute = True
            End With
            Process.Start(psi)
		Catch ex As System.Exception
            CMain.PopUpExc()
			CMain.DebugException(ex)
		End Try

    End Sub
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
    Private Sub B_Browse_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Browse.Click

		Try

            Dim fdlg As OpenFileDialog = New OpenFileDialog()
            fdlg.Title = Me.Text
            fdlg.FileName = Me.tx_DB.Text
            fdlg.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*"
            fdlg.FilterIndex = 1
            fdlg.RestoreDirectory = False
            If fdlg.ShowDialog() = DialogResult.OK Then
                Me.tx_DB.Text = fdlg.FileName 
            End If


		Catch ex As System.Exception
            CMain.PopUpExc()
			CMain.DebugException(ex)
		End Try


    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_FavAdd_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles B_FavAdd.Click

        AddOne()

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub TX_NewFav_KeyDown( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.KeyEventArgs) _
        Handles TX_NewFav.KeyDown

        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            AddOne()
        End If

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub LW_Fav_ColumnClick( _
        ByVal sender As System.Object,  _
        ByVal e As System.Windows.Forms.ColumnClickEventArgs) _
        Handles LW_Fav.ColumnClick

            '// code copied from
            '// http://www.vb-helper.com/howto_net_listview_sort_clicked_column.html

            ' Get the new sorting column.
            Dim new_sorting_column As ColumnHeader = _
                Me.LW_Fav.Columns(e.Column)

            ' Figure out the new sorting order.
            Dim sort_order As System.Windows.Forms.SortOrder
            If m_SortingColumn Is Nothing Then
                ' New column. Sort ascending.
                sort_order = SortOrder.Ascending
            Else
                ' See if this is the same column.
                If new_sorting_column.Equals(m_SortingColumn) Then
                    ' Same column. Switch the sort order.
                    If m_SortingColumn.Text.StartsWith("^") Then
                        sort_order = SortOrder.Descending
                    Else
                        sort_order = SortOrder.Ascending
                    End If
                Else
                    ' New column. Sort ascending.
                    sort_order = SortOrder.Ascending
                End If

                ' Remove the old sort indicator.
                m_SortingColumn.Text = _
                    m_SortingColumn.Text.Substring(2)
            End If

            ' Display the new sort order.
            m_SortingColumn = new_sorting_column
            If sort_order = SortOrder.Ascending Then
                m_SortingColumn.Text = "^ " & m_SortingColumn.Text
            Else
                m_SortingColumn.Text = "v " & m_SortingColumn.Text
            End If

            ' Create a comparer.
            Me.LW_Fav.ListViewItemSorter = New  _
                ListViewComparer(e.Column, sort_order)

            ' Sort.
            Me.LW_Fav.Sort()

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_FavRem_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles B_FavRem.Click

        If (Me.LW_Fav.SelectedItems.Count = 0) Then
            Autodesk.AutoCAD.ApplicationServices.Application. _
                ShowAlertDialog(CMain._T(TX.NOSELLI))
            Return
        End If

        For Each li As System.Windows.Forms.ListViewItem In Me.LW_Fav.SelectedItems
            Dim iIdx As Integer

            '// get the integer in question
            Try
                iIdx = CInt(li.Text)
            Catch ex As Exception
                Debug.Print("This should never happen! A non-numeric text in first column.")
            End Try
            For i As Integer = 0 to LocFavL.Length - 1
                If (LocFavL(i).idx = iIdx) Then
                    LocFavL(i).idx = 0
                End If
            Next
            li.Remove
        Next 
    End Sub
    '// ===================================================================== '

#End Region
    ' /////////////////////////////////////////////////////////////////////////

End Class

'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
